
/**
 *
 * @author Jaime Moreno 
 * Developed as a team based project for a course at the California State University of Monterey Bay by Reynold Rondez, Jaime Moreno, Chris Sterling, Eric McDonald
 *  SimpleLoop main program example using Java 2D for start of project for teambowser
 */


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;

public class DungeonMaster extends Applet implements Runnable,KeyListener, MouseListener   {

	 //random number generator
    Random rand = new Random();

    //the main thread
    Thread thread;

    //count the number of rectangles drawn
    long count = 0;

    //applet init event since an applet uses init instead of main like a regular java program
    public void init() {
        //not needed this time
    }

    //applet start event
    public void start() {
        thread = new Thread(this);
        thread.start();
    }

    //thread run event
    public void run() {
        long start = System.currentTimeMillis();

        //acquire the current thread
        Thread current = Thread.currentThread();

        //here's the new game loop
        while (current == thread) {
            try {
                Thread.sleep(0);
            }
            catch(InterruptedException e) {
                e.printStackTrace();
            }

            //draw something
            repaint();

            //figure out how fast it's running
            if (start + 1000 < System.currentTimeMillis()) {
                start = System.currentTimeMillis();
                showStatus("Rectangles per second: " + count);
                count = 0;
            }
        }
    }

    //applet stop event
    public void stop() {
        thread = null;
    }

    //applet update event
    public void update(Graphics g) {
        paint(g);
    }

    //applet paint event
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        //create a random rectangle
        int w = rand.nextInt(200);
        int h = rand.nextInt(200);
        int x = rand.nextInt(getSize().width - w);
        int y = rand.nextInt(getSize().height - h);
        Rectangle rect = new Rectangle(x,y,w,h);

        //generate a random color
        int red = rand.nextInt(256);
        int green = rand.nextInt(256);
        int blue = rand.nextInt(256);
        g2d.setColor(new Color(red,green,blue));

        //draw the rectangle
        g2d.fill(rect);

        //add another to the counter
        count++;
    }

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}


